package base.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.SparseArray
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import java.io.Serializable


abstract class BaseFragment: Fragment{
    protected var rootView: View? = null
    private var isInitView = false
    private var mActivity: BaseActivity? = null

    var mContext: Context? = null
    private var layoutId: Int = 0
    constructor(layoutId: Int) {
        this.layoutId = layoutId
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        rootView = inflater.inflate(layoutId, container, false)
        mContext = context
        return rootView
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        initView()
        isInitView = true
        initData()
    }

    override fun onAttach(context: Context) {
        super.onAttach(context)
        mActivity = context as BaseActivity
    }

    /*** 跳转相关  */
    fun readyGo(
        clazz: Class<*>?,
        key: String?,
        value: String?,
        requestCode: Int
    ) {
        val bundle = Bundle()
        bundle.putString(key, value)
        readyGo(clazz, bundle, requestCode)
    }

    fun readyGo(
        clazz: Class<*>?,
        key: String?,
        value: Boolean,
        requestCode: Int
    ) {
        val bundle = Bundle()
        bundle.putBoolean(key, value)
        readyGo(clazz, bundle, requestCode)
    }

    fun readyGo(
        clazz: Class<*>?,
        key: String?,
        value: Double,
        requestCode: Int
    ) {
        val bundle = Bundle()
        bundle.putDouble(key, value)
        readyGo(clazz, bundle, requestCode)
    }

    fun readyGo(
        clazz: Class<*>?,
        key: String?,
        value: Int,
        requestCode: Int
    ) {
        val bundle = Bundle()
        bundle.putInt(key, value)
        readyGo(clazz, bundle, requestCode)
    }

    fun readyGo(
        clazz: Class<*>?,
        key: String?,
        value: Serializable?,
        requestCode: Int
    ) {
        val bundle = Bundle()
        bundle.putSerializable(key, value)
        readyGo(clazz, bundle, requestCode)
    }

    fun readyGo(clazz: Class<*>?, requestCode: Int) {
        readyGo(clazz, null, requestCode)
    }

    @JvmOverloads
    fun readyGo(
        clazz: Class<*>?,
        bundle: Bundle? = null,
        requestCode: Int = -1
    ) {
        if (clazz != null) {
            val intent = Intent(activity, clazz)
            if (null != bundle) {
                intent.putExtras(bundle)
            }
            if (requestCode == -1) {
                startActivity(intent)
            } else {
                startActivityForResult(intent, requestCode)
            }
        }
    }

    /**
     * 让布局中的view与fragment中的变量建立起映射
     */
    protected abstract fun initView()

    /**
     * 加载要显示的数据
     */
    protected abstract fun initData()
}